<?php

namespace app\tiktok\logic;

use think\facade\Db;

class OrderLogic extends BasicLogic
{

    /**
     * @desc    达人基础信息列表
     * @date    2025-07-24
     * @author  luckyXiong
     */
    public function getExpertList($params=[]){
        $limit            = getIsset($params,'limit',10);
        $expert_name      = getIsset($params,'expert_name');
        $fb_admin_id      = getIsset($params,'fb_admin_id');
        $tags             = getIsset($params,'tags');
        $shop_id          = getIsset($params,'shop_id');
        $platform_id      = getIsset($params,'platform_id');
        $level            = getIsset($params,'level');
        $create_time      = getIsset($params,'create_time');
        $where            = [];

        if($fb_admin_id){
            $where[] = ['fb_admin_id', 'in', $fb_admin_id];
        }

        if($tags){
            $where[] =  ['tags', 'like', '%' . $tags . '%'];
        }

        if($expert_name){
            $where[] = ['expert_name', 'like', '%' . $expert_name . '%'];
        }

        if($shop_id){
            $where[]    = ['shop_id', 'in', $shop_id];
        }

        if($level){
            $where[] = ['level', '=',$level];
        }


        if($platform_id){
            $where[] = ['platform_id', '=',$platform_id];
        }

        if (!empty($create_time)) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        $field = "*";
        $res   = Db::name('tik_kol')
            ->field($field)
            ->where($where)
            ->order('sort DESC')
            ->order('create_time DESC')
            ->paginate($limit);
        $list = $res->items()?:[];

        foreach($list as $k=>$v){
            $links                  = $v['link'] ?  json_decode($v['link'],true):'';
            $link_arr               = self::get_links($links);
            $list[$k]['operater']   = self::get_fb_name($v['fb_admin_id']);
            $list[$k]['shop_name']  = self::get_shop_name($v['shop_id']);
            //合作价格
            $cooperate_price              = self::get_cooperate_price($v['expert_id']);
            $list[$k]['cooperate_price']  = $cooperate_price ? : '';
            //合作次数;
            $list[$k]['cooperate_nums']   = self::get_cooperate_nums($v['expert_id']);
            //合作品类;
            $list[$k]['cooperate_spu']    = self::get_cooperate_spu($v['expert_id']);
            $list[$k]['link']             = $links;
            $list[$k]['link_arr']         = $link_arr;
        }


        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc    获取店铺
     * @date    2025-07-24
     * @author  luckyXiong
     */
    public function getShop(){
        $shops       = Config('Logisticskey.tiktok_shop');
        $info        = getLevel($shops);
        foreach($info as $v){
            $arr[] = [
                'value'      => $v['key'],
                'label'      => $v['value'],
            ];
        }
        return  $this->jsonMessage(200,true,'获取成功!',$arr);
    }

    private function get_links($links){
        $arr = [];
        if($links){
            foreach($links as $k=>$v){
                if(strpos($v,'instagram')){
                    $arr[$k]['link']        = $v;
                    $arr[$k]['platform_id'] = 3;
                }elseif(strpos($v,'tiktok')){
                    $arr[$k]['link']        = $v;
                    $arr[$k]['platform_id'] = 1;
                }elseif(strpos($v,'youtube')){
                    $arr[$k]['link']        = $v;
                    $arr[$k]['platform_id'] = 2;
                }
            }
        }
        return  $arr;
    }





    /**
     * @desc    新增达人数据
     * @date    2025-07-24
     * @author  luckyXiong
     */
    public function addExpert($params=[]){
            $expert_name      =  $params['expert_name']??'';
            $email            =  $params['email']??'';
            $kol_family_name  =  $params['kol_family_name']??'';
            $kol_phone        =  $params['kol_phone']??'';
            $country          =  $params['country']??'';
            $address          =  $params['address']??'';
            $province         =  $params['province']??'';
            $city             =  $params['city']??'';
            $post_code        =  $params['post_code']??'';
            $link             =  $params['link']??'';
            $fan              =  $params['fan']??'';
            $quotation        =  $params['quotation']??'';
            $remarks          =  $params['remarks']??'';
            $fb_admin_id      =  $params['admin_id']??'';
            $platform_id      =  $params['platform_id']??'';
            $where            =  $platform_id_arr = [];
            $where[]          =  ['expert_name', 'like', '%' . $expert_name . '%'];
            if($link){
                  foreach($link as $k=>$v){
                           if(strpos($v,'instagram')){
                               $platform_id_arr[] = 3;
                           }elseif(strpos($v,'tiktok')){
                               $platform_id_arr[] = 1;
                           }elseif(strpos($v,'youtube')){
                               $platform_id_arr[] = 2;
                           }
                  }
                $link = json_encode($link);
            }

            if($platform_id_arr){
                $platform_id = implode(',',$platform_id_arr);
            }

            $insert_info = [
                'expert_name'     =>$expert_name,
                'email'           =>$email,
                'kol_family_name' =>$kol_family_name,
                'kol_phone'       =>$kol_phone,
                'country'         =>$country,
                'address'         =>$address,
                'province'        =>$province,
                'city'            =>$city,
                'post_code'       =>$post_code,
                'link'            =>$link,
                'fan'             =>$fan,
                'quotation'       =>$quotation,
                'remarks'         =>$remarks,
                'fb_admin_id'     =>$fb_admin_id,
                'platform_id'     =>$platform_id,
                'create_time'     =>date("Y-m-d H:i:s", time()),
                'update_time'     =>date("Y-m-d H:i:s", time()),
           ];
            $kol_info  =  Db::name('tik_kol')
            ->where($where)
            ->find();
            if(!$kol_info){
               $inset_id = Db::name('tik_kol')->insertGetId($insert_info);
               Db::name('tik_kol')->where(['id'=>$inset_id])->update(
                   [
                       'expert_id'=>'k_'.$inset_id
                   ]
               );
           }
           return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    达人审核接口
     * @date    2025-07-24
     * @author  luckyXiong
     */
    public function checkExpert($params=[]){
        $id               =  $params['id']??'';
        $check_status     =  $params['check_status']??'';
        Db::name('tik_kol_cooperate')->where(['id'=>$id])->update([
               'check_status' =>$check_status,
               'update_time' =>date("Y-m-d H:i:s", time()),
        ]);
        return  $this->jsonMessage(200,true,'操作成功!','');
    }



    /**
     * @desc    新增达人数据
     * @date    2025-07-24
     * @author  luckyXiong
     */
    public function editExpert($params=[]){
        $id               =  $params['id']??'';
        $expert_name      =  $params['expert_name']??'';
        $email            =  $params['email']??'';
        $kol_family_name  =  $params['kol_family_name']??'';
        $kol_phone        =  $params['kol_phone']??'';
        $country          =  $params['country']??'';
        $address          =  $params['address']??'';
        $province         =  $params['province']??'';
        $city             =  $params['city']??'';
        $post_code        =  $params['post_code']??'';
        $link             =  $params['link']??'';
        $tags             =  $params['tags']??'';
        $fan              =  $params['fan']??'';
        $quotation        =  $params['quotation']??'';
        $remarks          =  $params['remarks']??'';
        $fb_admin_id      =  $params['fb_admin_id']??'';
        $shop_id          =  $params['shop_id']??'';
        $platform_id      =  $params['platform_id']??'';
        $level            =  $params['level']??'';
        $is_cooperate     =  $params['is_cooperate']??'';
        $platform_id_arr  =  [];

        if($link){
            foreach($link as $k=>$v){
                if(strpos($v,'instagram')){
                    $platform_id_arr[] = 3;
                }elseif(strpos($v,'tiktok')){
                    $platform_id_arr[] = 1;
                }elseif(strpos($v,'youtube')){
                    $platform_id_arr[] = 2;
                }
            }
            $link = json_encode($link);
        }

        if($platform_id_arr){
            $platform_id = implode(',',$platform_id_arr);
        }


        $update_info = [
            'expert_name'     =>$expert_name,
            'email'           =>$email,
            'kol_family_name' =>$kol_family_name,
            'kol_phone'       =>$kol_phone,
            'country'         =>$country,
            'address'         =>$address,
            'province'        =>$province,
            'city'            =>$city,
            'post_code'       =>$post_code,
            'link'            =>$link,
            'fan'             =>$fan,
            'tags'            =>$tags,
            'quotation'       =>$quotation,
            'remarks'         =>$remarks,
            'shop_id'         =>$shop_id,
            'fb_admin_id'     =>$fb_admin_id,
            'platform_id'     =>$platform_id,
            'level'           =>$level,
            'is_cooperate'    =>$is_cooperate,
            'update_time'     =>date("Y-m-d H:i:s", time()),
        ];
        Db::name('tik_kol')->where(['id'=>$id])->update($update_info);
        return  $this->jsonMessage(200,true,'操作成功!','');
    }

    /**
     * @desc    删除达人
     * @date    2025-07-24
     * @author  luckyXiong
     */
    public function delExpert($params=[]){
        $id               =  $params['id']??'';
        Db::name('tik_kol')->where(['id'=>$id])->delete();
        return  $this->jsonMessage(200,true,'操作成功!','');
    }





    /**
     * @desc    获取达人列表接口
     * @date    2025-07-26
     * @author  luckyXiong
     */
    public function getExpert($params){
        $field    = 'expert_id,expert_name';
        $username = $params['username']??'';
        $where    = [];
        if($username){
            $username = trim($username);
            $where[] = ['expert_name', 'like', '%' . $username . '%'];
        }
         $list  = Db::name('tik_kol')->field($field)
                   ->where($where)
                   ->order('create_time desc')
                   ->select()
                   ->toArray();
         return  $this->jsonMessage(200,true,'获取成功!',$list);
    }




    /**
     * @desc    达人合作基础列表
     * @date    2025-07-26
     * @author  luckyXiong
     */
    public function getExpertCooperate($params){
        $limit              = getIsset($params,'limit',10);
        $expert_name        = getIsset($params,'expert_name');
        $fb_admin_id        = getIsset($params,'fb_admin_id');
        $status             = getIsset($params,'status');
        $spu                = getIsset($params,'spu');
        $shop_id            = getIsset($params,'shop_id');
        $platform_id        = getIsset($params,'platform_id');
        $is_out             = getIsset($params,'is_out');
        $cooperate_status   = getIsset($params,'cooperate_status');
        $fulfillment_status = getIsset($params,'fulfillment_status');
        $cooperate_type     = getIsset($params,'cooperate_type');
        $cooperate_time     = getIsset($params,'cooperate_time');
        $check_status       = getIsset($params,'check_status');
        $publish_time       = getIsset($params,'publish_time');
        $create_time        = getIsset($params,'create_time');
        $where              = [];

        if($fb_admin_id){
            $where[] = ['fb_admin_id', '=', $fb_admin_id];
        }

        if($shop_id){
            $where[] = ['shop_id', '=', $shop_id];
        }

        if($status){
            $where[] = ['status', '=', $status];
        }

        if($expert_name){
            $where[] = ['expert_name', 'like', '%' . $expert_name . '%'];
        }


        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }

        if($cooperate_type){
            $where[] = ['cooperate_type', '=',$cooperate_type];
        }

        if($cooperate_status){
            $where[] = ['cooperate_status', '=',$cooperate_status];
        }

        if($check_status){
            $where[] = ['check_status', '=',$check_status];
        }


        if($fulfillment_status){
            $where[] = ['fulfillment_status', '=',$fulfillment_status];
        }


        if($platform_id){
            $where[] = ['platform_id', '=',$platform_id];
        }

        if($is_out){
            $where[] = ['is_out', '=',$is_out];
        }



        if (!empty($cooperate_time)) {
            $where[]  = ['cooperate_time', '>=', date('Y-m-d H:i:s',strtotime($cooperate_time[0]))];
            $where[]  = ['cooperate_time', '<=', date('Y-m-d H:i:s',strtotime($cooperate_time[1]) + 3600*24-1)];
        }

        if (!empty($publish_time)) {
            $where[]  = ['publish_time', '>=', date('Y-m-d H:i:s',strtotime($publish_time[0]))];
            $where[]  = ['publish_time', '<=', date('Y-m-d H:i:s',strtotime($publish_time[1]) + 3600*24-1)];
        }


        if (!empty($create_time)) {
            $where[]  = ['created_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['created_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }


        $field = "*";
        $res   = Db::name('tik_kol_cooperate')
            ->field($field)
            ->where($where)
            ->order('create_time DESC')
            ->paginate($limit);
        $list = $res->items()?:[];

        //批量获取创建人;
        $operater_ids    = array_column($list,'fb_admin_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        $shops           = Config('Logisticskey.tiktok_shop');
        foreach($list as $k=>$v){
            $admin_users_info      = $admin_users_map[$v['fb_admin_id']]??'';
            $list[$k]['operater']  = $admin_users_info['username']??'';
            $list[$k]['shop_name'] = $shops[$v['shop_id']]??'';
            $list[$k]['cooperate_time'] = $v['cooperate_time'] ?  date("Y-m-d", strtotime($v['cooperate_time'])) : '';
        }


        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }



    /**
     * @desc    新增达人合作接口
     * @date    2025-07-26
     * @author  luckyXiong
     */
    public function addExpertCooperate($params){
         $shop_id           = $params['shop_id']??'';
         $expert_id         = $params['expert_id']??'';
         $creation_code     = $params['creation_code']??'';
         $platform_id       = $params['platform_id']??'';
         $cooperate_status  = $params['cooperate_status']??'';
         $cooperate_type    = $params['cooperate_type']??'';
         $link              = $params['link']??'';
         $spu               = $params['spu']??'';
         $sku               = $params['sku']??'';
         $is_out            = $params['is_out']??'0';
         $payment_price     = $params['payment_price']??'';
         $order_number      = $params['order_number']??'';
         $track_number      = $params['track_number']??'';
         $publish_url       = $params['publish_url']??'';
         $fb_admin_id       = $params['fb_admin_id']??'';
         $cooperate_time    = $params['cooperate_time']??null;
         $expire_time       = $params['expire_time']??null;
         $publish_time      = $params['publish_time']??null;
         $expert_name       = Db::name('tik_kol_cooperate')
                             ->where(['expert_id'=>$expert_id])
                             ->value('expert_name');
         $insert_data       = [
                   'shop_id'         =>$shop_id,
                   'expert_id'       =>$expert_id,
                   'expert_name'     =>$expert_name??'',
                   'creation_code'   =>$creation_code,
                   'platform_id'     =>$platform_id,
                   'cooperate_status'=>$cooperate_status,
                   'cooperate_type'  =>$cooperate_type,
                   'link'            =>$link,
                   'spu'             =>$spu,
                   'sku'             =>$sku,
                   'is_out'          =>$is_out,
                   'order_number'    =>$order_number,
                   'track_number'    =>$track_number,
                   'publish_url'     =>$publish_url,
                   'payment_price'   =>$payment_price,
                   'fb_admin_id'     =>$fb_admin_id,
                   'cooperate_time'  =>$cooperate_time,
                   'expire_time'     =>$expire_time,
                   'publish_time'    =>$publish_time,
                   'create_time'     =>date("Y-m-d H:i:s", time()),
                   'update_time'     =>date("Y-m-d H:i:s", time()),
          ];
          Db::name('tik_kol_cooperate')->insert($insert_data);
          return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    编辑达人
     * @date    2025-07-26
     * @author  luckyXiong
     */
    public function editExpertCooperate($params){
        $id                = $params['id']??'';
        $shop_id           = $params['shop_id']??'';
        $expert_id         = $params['expert_id']??'';
        $creation_code     = $params['creation_code']??'';
        $platform_id       = $params['platform_id']??'';
        $cooperate_status  = $params['cooperate_status']??'';
        $cooperate_type    = $params['cooperate_type']??'';
        $link              = $params['link']??'';
        $spu               = $params['spu']??'';
        $sku               = $params['sku']??'';
        $is_out            = $params['is_out']??'0';
        $payment_price     = $params['payment_price']??'';
        $order_number      = $params['order_number']??'';
        $track_number      = $params['track_number']??'';
        $fulfillment_status= $params['fulfillment_status']??'';
        $publish_url       = $params['publish_url']??'';
        $storage_url       = $params['storage_url']??'';
        $fb_admin_id       = $params['fb_admin_id']??'';
        $cooperate_time    = $params['cooperate_time']??null;
        $expire_time       = $params['expire_time']??null;
        $publish_time      = $params['publish_time']??null;
        $expert_name       = Db::name('tik_kol_cooperate')
            ->where(['expert_id'=>$expert_id])
            ->value('expert_name');
        $update_data       = [
            'shop_id'         =>$shop_id,
            'expert_id'       =>$expert_id,
            'expert_name'     =>$expert_name??'',
            'creation_code'   =>$creation_code,
            'platform_id'     =>$platform_id,
            'cooperate_status'=>$cooperate_status,
            'cooperate_type'  =>$cooperate_type,
            'link'            =>$link,
            'spu'             =>$spu,
            'sku'             =>$sku,
            'is_out'          =>$is_out,
            'order_number'    =>$order_number,
            'track_number'    =>$track_number,
            'publish_url'     =>$publish_url,
            'storage_url'     =>$storage_url,
            'payment_price'   =>$payment_price,
            'fb_admin_id'     =>$fb_admin_id,
            'cooperate_time'     =>$cooperate_time,
            'fulfillment_status' =>$fulfillment_status,
            'expire_time'     =>$expire_time,
            'publish_time'    =>$publish_time,
            'update_time'     =>date("Y-m-d H:i:s", time()),
        ];
        Db::name('tik_kol_cooperate')->where(['id'=>$id])->update($update_data);
        return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    删除合作达人接口
     * @date    2025-07-26
     * @author  luckyXiong
     */
    public function delExpertCooperate($params){
        $id    = $params['id']??'';
        Db::name('tik_kol_cooperate')->where(['id'=>$id])->delete();
        return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    达人合作样品订单
     * @date    2025-07-27
     * @author  luckyXiong
     */
    public function cooperateOrder($params){
        $limit              = getIsset($params,'limit',10);
        $expert_name        = getIsset($params,'expert_name');
        $fb_admin_id        = getIsset($params,'fb_admin_id');
        $order_id           = getIsset($params,'order_id');
        $fulfillment_status = getIsset($params,'fulfillment_status');
        $track_number       = getIsset($params,'track_number');
        $publish_time       = getIsset($params,'publish_time');
        $receive_time       = getIsset($params,'receive_time');
        $create_time        = getIsset($params,'create_time');
        $where              = [];

        if($fb_admin_id){
            $where[] = ['fb_admin_id', '=', $fb_admin_id];
        }

        if($expert_name){
            $where[] = ['expert_name', 'like', '%' . $expert_name . '%'];
        }


        if($order_id){
            $where[] =  ['order_id', '=',$order_id];
        }


        if($fulfillment_status){
            $where[] =  ['fulfillment_status', '=',$fulfillment_status];
        }

        if($track_number){
            $where[] =  ['track_number', '=',$track_number];
        }


        if($publish_time){
            $where[]  = ['created_time', '>=', date('Y-m-d H:i:s',strtotime($publish_time[0]))];
            $where[]  = ['created_time', '<=', date('Y-m-d H:i:s',strtotime($publish_time[1]) + 3600*24-1)];
        }


        if($receive_time){
            $where[]  = ['created_time', '>=', date('Y-m-d H:i:s',strtotime($receive_time[0]))];
            $where[]  = ['created_time', '<=', date('Y-m-d H:i:s',strtotime($receive_time[1]) + 3600*24-1)];
        }



        if (!empty($create_time)) {
            $where[]  = ['created_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['created_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        $field = "*";
        $res   = Db::name('tik_kol_order')
            ->field($field)
            ->where($where)
            ->order('create_time DESC')
            ->paginate($limit);
        $list = $res->items()?:[];

        //批量获取创建人;
        $operater_ids    = array_column($list,'fb_admin_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        foreach($list as $k=>$v){
            $admin_users_info          = $admin_users_map[$v['fb_admin_id']]??'';
            $list[$k]['operater']      = $admin_users_info['username']??'';
            $list[$k]['platform_track']= $v['track_number'] ? self::get_track_type($v['track_number']):0;
        }


        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    private  function get_track_type($track_number){
        if (preg_match('/(DYY)/', $track_number)) {
            return  1;
        } elseif(preg_match('/(4PX)/', $track_number))  {
            return  2;
        }else{
            return  0;
        }

    }


    /**
     * @desc    达人合作样品订单新增
     * @date    2025-07-27
     * @author  luckyXiong
     */
    public function addCooperateOrder($params){
        $shop_id           = $params['shop_id']??'';
        $expert_id         = $params['expert_id']??'';
        $order_id          = $params['order_id']??'';
        $fb_admin_id       = $params['fb_admin_id']??'';
        $link              = $params['link']??'';
        $spu               = $params['spu']??'';
        $sku               = $params['sku']??'';
        $track_name        = $params['track_name']??'';
        $track_number      = $params['track_number']??'';
        $fulfillment_status = $params['fulfillment_status']??'';
        $remarks           = $params['remarks']??'';
        $remark_exception  = $params['remark_exception']??'';
        $customer_name     = $params['customer_name']??'';
        $email             = $params['email']??'';
        $phone             = $params['phone']??'';
        $country           = $params['country']??'';
        $province          = $params['province']??'';
        $city              = $params['city']??'';
        $zip               = $params['zip']??'';
        $address           = $params['address']??'';
        $publish_time      = $params['publish_time']??null;
        $cooperate_time    = $params['cooperate_time']??null;
        $receive_time      = $params['receive_time']??null;
        $last_time         = $params['last_time']??null;
        $expert_name       = Db::name('tik_kol')
            ->where(['expert_id'=>$expert_id])
            ->value('expert_name');
        $insert_data       = [
            'shop_id'           =>$shop_id,
            'expert_id'         =>$expert_id,
            'expert_name'       =>$expert_name??'',
            'order_id'          =>$order_id,
            'fb_admin_id'       =>$fb_admin_id,
            'link'              =>$link,
            'spu'               =>$spu,
            'sku'               =>$sku,
            'track_name'        =>$track_name,
            'track_number'      =>$track_number,
            'fulfillment_status'=>$fulfillment_status,
            'remarks'           =>$remarks,
            'cooperate_time'    =>$cooperate_time,
            'customer_name'     =>$customer_name,
            'email'             =>$email,
            'phone'             =>$phone,
            'country'           =>$country,
            'province'          =>$province,
            'city'              =>$city,
            'zip'               =>$zip,
            'address'           =>$address,
            'publish_time'      =>$publish_time,
            'receive_time'      =>$receive_time,
            'last_time'         =>$last_time,
            'create_time'       =>date("Y-m-d H:i:s", time()),
            'update_time'       =>date("Y-m-d H:i:s", time()),
        ];
        Db::name('tik_kol_order')->insert($insert_data);
        return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    达人合作样品订单编辑
     * @date    2025-07-27
     * @author  luckyXiong
     */
    public function editCooperateOrder($params){
        $id                = $params['id']??'';
        $shop_id           = $params['shop_id']??'';
        $expert_id         = $params['expert_id']??'';
        $order_id          = $params['order_id']??'';
        $fb_admin_id       = $params['fb_admin_id']??'';
        $link              = $params['link']??'';
        $spu               = $params['spu']??'';
        $sku               = $params['sku']??'';
        $track_name        = $params['track_name']??'';
        $track_number      = $params['track_number']??'';
        $fulfillment_status = $params['fulfillment_status']??'';
        $remarks           = $params['remarks']??'';
        $remark_exception  = $params['remark_exception']??'';
        $customer_name     = $params['customer_name']??'';
        $email             = $params['email']??'';
        $phone             = $params['phone']??'';
        $country           = $params['country']??'';
        $province          = $params['province']??'';
        $city              = $params['city']??'';
        $zip               = $params['zip']??'';
        $address           = $params['address']??'';
        $publish_time      = $params['publish_time']??null;
        $cooperate_time    = $params['cooperate_time']??null;
        $receive_time      = $params['receive_time']??null;
        $last_time         = $params['last_time']??null;
        $expert_name       = Db::name('tik_kol')
            ->where(['expert_id'=>$expert_id])
            ->value('expert_name');
        $update_data       = [
            'shop_id'           =>$shop_id,
            'expert_id'         =>$expert_id,
            'expert_name'       =>$expert_name??'',
            'order_id'          =>$order_id,
            'fb_admin_id'       =>$fb_admin_id,
            'link'              =>$link,
            'spu'               =>$spu,
            'sku'               =>$sku,
            'track_name'        =>$track_name,
            'track_number'      =>$track_number,
            'fulfillment_status'=>$fulfillment_status,
            'remarks'           =>$remarks,
            'remark_exception'  =>$remark_exception,
            'cooperate_time'    =>$cooperate_time,
            'customer_name'     =>$customer_name,
            'email'             =>$email,
            'phone'             =>$phone,
            'country'           =>$country,
            'province'          =>$province,
            'city'              =>$city,
            'zip'               =>$zip,
            'address'           =>$address,
            'publish_time'      =>$publish_time,
            'receive_time'      =>$receive_time,
            'last_time'         =>$last_time,
            'update_time'       =>date("Y-m-d H:i:s", time()),
        ];
        Db::name('tik_kol_order')->where(['id'=>$id])->update($update_data);
        return  $this->jsonMessage(200,true,'操作成功!','');
    }



    /**
     * @desc    达人合作样品订单删除
     * @date    2025-07-27
     * @author  luckyXiong
     */
    public function delCooperateOrder($params){
        $id = $params['id']??'';
        Db::name('tik_kol_order')->where(['id'=>$id])->delete();
        return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    达人素材广告列表
     * @date    2025-07-29
     * @author  luckyXiong
     */
    public function videoAdvList($params){
           $list = Db::name('tik_kol_copy')->group('expert_name')->select()->toArray();
           $arr  = [];
           foreach($list as $v){
                   $temp = [
                       'expert_id'   =>$v['expert_id']??'',
                       'expert_name' =>$v['expert_name']??'',
                       'email'       =>$v['email']??'',
                       'link'        =>$v['link']??'',
                       'type'        =>$v['type']??'',
                       'platform_id' =>$v['platform_id']??'',
                       'fb_admin_id' =>$v['platform_id']??'',
                       'is_cooperate'=>$v['is_cooperate']??'1',
                       'update_time' =>date("Y-m-d H:i:s", time()),
                       'create_time' =>date("Y-m-d H:i:s", time()),
                   ];
                   $arr[] = $temp;
             }
            Db::name('tik_kol')->insertAll($arr);
            return  $this->jsonMessage(200,true,'操作成功!','');
      }


    /**
     * @desc    自动刷新达人ID
     * @date    2025-08-05
     * @author  luckyXiong
     */
    public function get_new_expert(){
           $field = 'id,expert_id';
           $list  = Db::name('tik_kol')->field($field)->order('id asc')->select()->toArray();
           foreach($list as $k=>$v){
               $nextNumber    = $k+1;
               $nextId        = 'kol_' . str_pad($nextNumber, 5, '0', STR_PAD_LEFT);
               Db::name('tik_kol')->where(['id'=>$v['id']])->update(
                   [
                       'expert_id'=>$nextId
                   ]
               );
           }
          return  $this->jsonMessage(200,true,'操作成功!','');
    }


    /**
     * @desc    合作记录弹框
     * @date    2025-08-07
     * @author  luckyXiong
     */
    public function get_cooperate_info($params){
        $expert_id = $params['expert_id']??'';
        $list      = Db::name('tik_kol_cooperate')->where(['expert_id'=>$expert_id])->select()->toArray();

        //批量获取创建人;
        $operater_ids    = array_column($list,'fb_admin_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        //批量获取联系方式
        $expert_ids      = array_column($list,'expert_id');
        $expert_users    = Db::name('tik_kol')->field('id,expert_id,email')->whereIn('expert_id',$expert_ids)->select()->toArray();
        $email_map       = array_column($expert_users,null,'expert_id');

        $arr_list = [];
        foreach($list as $k=>$v){
            $admin_users_info               = $admin_users_map[$v['fb_admin_id']]??'';
            $email_map_info                 = $email_map[$v['expert_id']]??'';
            $arr_list[$k]['expert_id']      = $v['expert_id']??'';
            $arr_list[$k]['expert_name']    = $v['expert_name']??'';
            $arr_list[$k]['link']           = $v['link']??'';
            $arr_list[$k]['cooperate_time'] = $v['cooperate_time']??'';
            $arr_list[$k]['operater']       = $admin_users_info['username']??'';
            $arr_list[$k]['platform_id']    = $v['platform_id']??'';
            $arr_list[$k]['email']          = $email_map_info['email']??'';
            $arr_list[$k]['cooperate_type'] = $v['cooperate_type']??'';
            $arr_list[$k]['cooperate_status']= $v['cooperate_status']??'';
            $arr_list[$k]['spu']            = $v['spu']??'';
            $arr_list[$k]['sku']            = $v['sku']??'';
            $arr_list[$k]['fulfillment_status']    = $v['fulfillment_status']??'';
            $arr_list[$k]['expire_time']        = $v['expire_time']??'';
            $arr_list[$k]['remarks']            = $v['remarks']??'';
            $arr_list[$k]['is_out']             = $v['is_out']??'';
        }
        return  $this->jsonMessage(200,true,'获取成功!',$arr_list);
    }


    /**
     * @desc    获取标签列表
     * @date    2025-08-06
     * @author  luckyXiong
     */
    public function get_tags_info($params){
           $list =  Db::name('tik_kol_tag')->select()->toArray();
           return  $this->jsonMessage(200,true,'获取成功!',$list);
    }


    /**
     * @desc    达人标签操作
     * @date    2025-08-06
     * @author  luckyXiong
     */
    public function operate_tags($params){
           $id       = $params['id']??'';
           $tags     = $params['tags']??'';
           $new_tags = [];
           if($tags){
               $tags     = trim($tags);
               $new_tags = explode(',',$tags);
           }
           $old_tags  = Db::name('tik_kol_tag')->column('tag_name');
           $diff1     = $arr = [];
           if($new_tags){
               $diff1 = array_diff($new_tags, $old_tags); // 这会得到 "9月新增", "合作费用偏高"
           }

           if($diff1){
                 foreach($diff1 as $v){
                        $temp['tag_name']    = $v;
                        $temp['create_time'] = date("Y-m-d H:i:s", time());
                        $arr[]               = $temp;
                 }
               Db::name('tik_kol_tag')->insertAll($arr);
           }

           if($tags){
               Db::name('tik_kol')->where(['id'=>$id])->update(
                   ['tags'=>$tags]
               );
           }
          return  $this->jsonMessage(200,true,'操作成功!','');
    }






    /**
     * @desc    自动刷新达人ID
     * @date    2025-08-05
     * @author  luckyXiong
     */
    public function get_expert_id(){
        $currentMaxId  = Db::name('tik_kol')->order('id desc')->value('expert_id');
        $currentNumber = (int)substr($currentMaxId, strlen('kol_'));
        $nextNumber    = $currentNumber + 1;
        $nextId        = 'kol_' . str_pad($nextNumber, 5, '0', STR_PAD_LEFT);
        return $nextId;
    }



    /**
     * @desc   历史数据列表
     * @author luckyXiong
     * @date   2024-11-06
     */
    public function get_shop_name($admin_ids){
        if(!$admin_ids)  return '';
        //批量获取运营名称;
        $admin_names  = Db::name('shops')->whereIn('id',$admin_ids)->column('name');
        return $admin_names ? implode(',',$admin_names) : '';
    }

    /**
     * @desc   历史数据列表
     * @author luckyXiong
     * @date   2024-11-06
     */
    public function get_fb_name($admin_ids){
        if(!$admin_ids)  return '';
        //批量获取运营名称;
        $admin_names   = Db::name('system_admin')->whereIn('id',$admin_ids)->column('username');
        return $admin_names ? implode(',',$admin_names) : '';
    }

    public function get_cooperate_price($expert_id){
          $price = Db::name('tik_kol_cooperate')->where(['expert_id'=>$expert_id])->order('create_time desc')->value('payment_price');
          return $price ?:'';
    }


    public function get_cooperate_nums($expert_id){
        $nums = Db::name('tik_kol_cooperate')->where(['expert_id'=>$expert_id])->count();
        return $nums ?:0;
    }

    public function get_cooperate_spu($expert_id){
        $spus = Db::name('tik_kol_cooperate')->where(['expert_id'=>$expert_id])->column('spu');
        if($spus){
            $spus = implode(',',$spus);
        }
        return $spus;
    }









}